<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <draggable v-model="form.data" animation="300" @end="clone">
        <el-col v-for="(item,index) in form.data" :key="'data-'+index" style="border-bottom: 1px solid #e2e2e2;margin-bottom: 10px" :span="24">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                label="请选择数据项"
                :prop="'data.'+index+'.number'"
                :rules="[{ required: true, message: '请选择数据项', trigger: 'change' }]"
              >
                <el-select v-model="item.number" clearable placeholder="请选择数据项">
                  <el-option
                    v-for="obj in dataList"
                    :key="obj.id"
                    :label="obj.title"
                    :value="obj.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="text-align: center">
              <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
      </draggable>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { updateObject } from '@/api/portal/manager/columnTemp';
import { queryAll } from '@/api/carousel/index';
import draggable from 'vuedraggable';
export default {
  name: 'PcCarouselTempV1',
  components: {
    draggable
  },
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      dataList: [],
      form: {
        data: []
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx);
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault();
      event.stopPropagation();
    };
    this.initAllCarousel();
  },
  methods: {
    doAdd() {
      const item = { px: 0, number: null };
      this.form.data.push(item);
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item);
      if (index !== -1) {
        this.form.data.splice(index, 1);
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1;
        return item;
      });
    },
    initAllCarousel() {
      queryAll().then(res => {
        const { data } = res;
        this.dataList = data;
      }).catch(() => {
        this.dataList = [];
      });
    },
    save() {
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data).then(res => {
            this.close();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>

<style scoped>
.setting-body {
  width: 100%;
}
.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
